<template>
  <div class="container">
    <div class="title">新建仓库</div>
    <div class="wrap">
      <el-row>
        <el-col :lg="16" :md="20" :sm="24" :xs="24">
          <el-form :model="form" :rules="rules" status-icon ref="form" label-width="100px" @submit.native.prevent>
            <el-form-item label="区域名称" prop="name">
              <el-input size="medium" v-model="form.name" placeholder="请填写区域名称"></el-input>
            </el-form-item>
            <el-form-item label="区域优先级" prop="area_order">
              <el-input size="medium" v-model="form.area_order" placeholder="请填写区域优先级"></el-input>
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="form.receive_flag">接收</el-checkbox>
              <el-checkbox v-model="form.stock_flag">仓储</el-checkbox>
              <el-checkbox v-model="form.pick_flag">拣货</el-checkbox>
              <el-checkbox v-model="form.return_flag">退货</el-checkbox>
            </el-form-item>
            <el-form-item class="submit">
              <el-button type="primary" @click="submitForm('form')" :loading="loading">保 存</el-button>
              <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import warehouse from '@/model/warehouse'

export default {
  props: {
    codes: {
      type: String,
    },
  },
  data() {
    return {
      form: {
        name: "",
        area_order: "",
        receive_flag: false,
        stock_flag: false,
        pick_flag: false,
        return_flag: false,
        code: ''
      },
      regionals: null,
      countries: null,
      loading: false,
      regional: [],
      area: [],
      county: [],
      rules: {
        name: [
          { required: true, message: '请输入仓库名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
        ],
        area_order: [{ required: true, message: '请输入区域优先级', trigger: 'blur' }],
      },
    }
  },
  created() {},
  methods: {
    async submitForm(formName) {
      try {
        this.loading = true
        console.log(this.form)
        console.log(this.codes);
        this.form.code = this.codes
        const res = await warehouse.addArea(this.form)
        this.loading = false
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.$message.success(`${res.message}`)
          this.resetForm(formName)
          this.$emit('addCloses')
        }
      } catch (error) {
        this.loading = false
        this.$message.error('区域添加失败，请检测填写信息')
        console.log(error)
      }
    },

    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;
    border-bottom: 1px solid #dae1ec;
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
.box {
  display: flex;
}
</style>
